/**
 * 
 */
package ro.dandanciu.webgallery.services.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import ro.dandanciu.webgallery.constants.GalleriesConstants;
import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.GalleryServices;
import ro.dandanciu.webgallery.services.ImageServices;

/**
 * @author Dan
 *
 *
 */
@Transactional
public class GalleryServicesImpl implements GalleryServices {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(GalleryServicesImpl.class);

	private EntityManager em;
	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	private ImageServices imageServices;
	
	/**
	 * @param imageServices the imageServices to set
	 */
	public void setImageServices(ImageServices imageServices) {
		this.imageServices = imageServices;
	}

	/* (non-Javadoc)
	 * @see ro.dandanciu.webgallery.services.GalleryServices#getGalleriesByOwner(ro.dandanciu.webgallery.domainmodel.User)
	 */
	@SuppressWarnings("unchecked")
	public List<Gallery> getGalleriesByOwner(User owner, long galleryId, String filter) {
		if (filter == null) {
			filter = "";
		}
		filter += "%";
		
		List<Gallery> galleries = new ArrayList<Gallery>();
		if (galleryId < 0) {
			Query query = em.createNamedQuery("Gallery.findRootByOwner");
			query.setParameter("userId", owner.getUserId());
			query.setParameter("filter", filter);
			galleries = (List<Gallery>) query.getResultList();
		} else {
			Query query = em.createNamedQuery("Gallery.findByOwnerAndParent");
			query.setParameter("userId", owner.getUserId());
			query.setParameter("galleryId", galleryId);
			query.setParameter("filter", filter);
			galleries = (List<Gallery>) query.getResultList();
		}
		return galleries;
	}

	/* (non-Javadoc)
	 * @see ro.dandanciu.webgallery.services.GalleryServices#save(ro.dandanciu.webgallery.domainmodel.Gallery)
	 */
	
	public void save(Gallery gallery) {
		gallery.setUpdater(gallery.getOwner().getUserId());
		em.persist(gallery);
	}

	public Gallery getGalleryById(long galleryId) {
		return em.find(Gallery.class, galleryId);
	}

	@SuppressWarnings("unchecked")
	public List<Gallery> getPublicGalleries(String filter) {
		Query query = em.createNamedQuery("Gallery.findAllPublic");
		query.setParameter("filter", filter + "%");
		return (List<Gallery>) query.getResultList();
	}

	
	public void update(Gallery gallery) {
		gallery.setUpdater(gallery.getOwner().getUserId());
		em.merge(gallery);
	}
	
	@SuppressWarnings("unchecked")
	public Gallery getGalleryByOwnerAndName(String ownerUserId, String galleryName) {
		Query query = em.createNamedQuery("Gallery.findByOwnerAndName");
		query.setParameter("ownerUserId", ownerUserId);
		query.setParameter("galleryName", galleryName);
		List<Gallery> resultList = query.getResultList();
		Gallery gallery = null;
		if (resultList.size() > 0) {
			gallery = (Gallery) resultList.get(0);
		}
		return gallery;
	}
	
	public String getFsPathForGallery(Gallery gallery) {
		if (logger.isDebugEnabled()) {
			logger.debug("getFsPathForGallery(Gallery) - start");
			logger.debug("gallery: " + gallery); 
		}

		String path = "/";
		Gallery tempGallery = gallery;
		while (tempGallery != null) {
			path = "/" + tempGallery.getName().trim() + path;
			tempGallery = tempGallery.getParent();
		}
		

		String returnString = GalleriesConstants.GALLERIES_BASE_DIRECTORY + "/" + gallery.getOwner().getUserId() + path;
		if (logger.isDebugEnabled()) {
			logger.debug("getFsPathForGallery(Gallery) - end");
			logger.debug("return: " + returnString);
		}
		return returnString ;
	}

	@Override
	public void delete(Long... ids) {

		for (Long id : ids) {
			Gallery gallery = em.find(Gallery.class, id);
			if (gallery != null) {
				em.remove(gallery);
			} else {
				logger.warn("Received for delete, but did not find gallery with id: " + id);
			}
		}
		
	}
}
